﻿using System.Collections.ObjectModel;

namespace TfsUtils.Ui.Mvvm.Command
{
	public class ObservableCollectionMonitor
	{
		public static ObservableCollectionMonitor<T> Monitor<T>(ObservableCollection<T> source)
		{
			return new ObservableCollectionMonitor<T>(source);
		}

	}
	public class ObservableCollectionMonitor<T> : AbstractMonitor<ObservableCollection<T>>
	{
		public ObservableCollectionMonitor(ObservableCollection<T> source)
			: base(source)
		{
			base.WeakSource.Target.CollectionChanged += CollectionChanged;
		}

		void CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
		{
			base.NotifyChanged();
		}

		protected override void OnStopMonitoring()
		{
			if (this.WeakSource != null && this.WeakSource.IsAlive)
			{
				this.WeakSource.Target.CollectionChanged -= CollectionChanged;
			}
		}
	}
}